package com.example.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.shop.DBHelper;
import com.example.shop.Commodity;

import java.util.ArrayList;

public class CommodityDataUtils {
    private static SQLiteDatabase db;
    private static CommodityDataUtils instance = null;

    public CommodityDataUtils(Context context){

        DBHelper helper = new DBHelper(context);
        db = helper.getWritableDatabase();
    }
    public static CommodityDataUtils getInstance(Context context) {
        if (instance == null) {
            instance = new CommodityDataUtils(context);
        }
        return instance;
    }
    public void addCommodity(Commodity commodity){
        ContentValues cv = new ContentValues();
        cv.put("name",commodity.item_name);
        cv.put("price",commodity.item_price);
        cv.put("profile",commodity.item_profile);
        cv.put("image",commodity.item_image);
        db.insert(DBHelper.U_COMMODITY,null,cv);
    }

    public ArrayList<Commodity> getAllCommodity(){
        Cursor cursor = db.query(DBHelper.U_COMMODITY,null,null,null,null,null,null);
        ArrayList<Commodity> arrayList = new ArrayList<>();
        while (cursor.moveToNext()) {
            Commodity commodity = new Commodity();
            commodity.item_id = cursor.getInt(cursor.getColumnIndex("_id"));
            commodity.item_price = cursor.getString(cursor.getColumnIndex("price"));
            commodity.item_name = cursor.getString(cursor.getColumnIndex("name"));
            commodity.item_profile = cursor.getString(cursor
                    .getColumnIndex("profile"));
            commodity.item_image = cursor.getString(cursor.getColumnIndex("image"));
            arrayList.add(commodity);
        }
        cursor.close();
        return arrayList;
    }
}
